package cn.iocoder.yudao.module.famous.doctor.service.question;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.question.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.question.FamQuestionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.question.FamQuestionConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.question.FamQuestionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 常见问题 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FamQuestionServiceImpl implements FamQuestionService {

    @Resource
    private FamQuestionMapper famQuestionMapper;

    @Override
    public Long createFamQuestion(FamQuestionCreateReqVO createReqVO) {
        // 插入
        FamQuestionDO famQuestion = FamQuestionConvert.INSTANCE.convert(createReqVO);
        famQuestionMapper.insert(famQuestion);
        // 返回
        return famQuestion.getId();
    }

    @Override
    public void updateFamQuestion(FamQuestionUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamQuestionExists(updateReqVO.getId());
        // 更新
        FamQuestionDO updateObj = FamQuestionConvert.INSTANCE.convert(updateReqVO);
        famQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamQuestion(Long id) {
        // 校验存在
        validateFamQuestionExists(id);
        // 删除
        famQuestionMapper.deleteById(id);
    }

    private void validateFamQuestionExists(Long id) {
        if (famQuestionMapper.selectById(id) == null) {
            throw exception(FAM_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public FamQuestionDO getFamQuestion(Long id) {
        return famQuestionMapper.selectById(id);
    }

    @Override
    public List<FamQuestionDO> getFamQuestionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famQuestionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamQuestionDO> getFamQuestionPage(FamQuestionPageReqVO pageReqVO) {
        return famQuestionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamQuestionDO> getFamQuestionList(FamQuestionExportReqVO exportReqVO) {
        return famQuestionMapper.selectList(exportReqVO);
    }

}
